package com.chanyachting.emall.action.hr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Sellyacht;
import com.chanyachting.emall.form.CompanyForm;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.SellyachtService;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.SellyachtServiceImpl;

public class ReadCompanyOfProdList1Action  extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//一般页面记录数
		int COMMON_PAGE_SIZE = 20;
		
		CompanyService comService = new CompanyServiceImpl();
		SellyachtService syService = new SellyachtServiceImpl();

		CompanyForm tForm = (CompanyForm)form;
		if(tForm.getId() == null || "".equals(tForm.getId())) {
			return mapping.findForward("error");
		}
		int currentPage = tForm.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		
		
		List<Company> alList = null;
		List<Sellyacht> syList = null;
		alList = comService.getCompanyById(tForm.getId().toString());
		
		if (alList.size() == 0) {
			return mapping.findForward("error");
		}
		int recordCnt = syService.getSellyachtCount(String.valueOf(alList.get(0).getId()));
		
		int pageCount = recordCnt%pageSize == 0?recordCnt/pageSize:(recordCnt/pageSize + 1);
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		
		
		tForm.setComp(alList.get(0));
		syList = syService.getSellyachtListByPage(currentPage,pageSize,alList.get(0).getId().toString());
		tForm.setSyList(syList);
		return mapping.findForward("readProdList1");
	}
}
